package cn.com.jrj.base.util;

import java.text.DecimalFormat;
import java.text.Format;

public class DecimalUtil {

	private final static DecimalFormat DEFAULT_FORMAT = new DecimalFormat("#.##");

	private static final DecimalUtil instance = new DecimalUtil();

	public static DecimalUtil getInstance() {
		return instance;
	}

	public String format(Object data) {
		if (data instanceof Number)
			return DEFAULT_FORMAT.format((Number) data);
		else
			return "";
	}

	public String format(Object data, String f) {
		if (data instanceof Number) {
			try {
				DecimalFormat format = new DecimalFormat(f);
				return format.format((Number) data);
			} catch (Exception e) {
			}
		}
		return "";
	}

	// 格式化数据
	public static String formatNumber(String str) {
		if (str == null) {
			str = "";
		} else {
			Format decimalFormat = new DecimalFormat("0.0000");
			str = decimalFormat.format(Double.parseDouble(str));
		}
		return str;
	}

	// 格式化数据
	public static String formatNumber(String str, String format) {
		if (str == null) {
			str = "";
		} else {
			Format decimalFormat = new DecimalFormat(format);
			str = decimalFormat.format(Double.parseDouble(str));
		}
		return str;
	}
}
